<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>💹个性化投资模拟AI</title>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --accent-color: #2ecc71;
            --light-bg: #f8f9fa;
            --dark-text: #2c3e50;
            --light-text: #ecf0f1;
            --border-radius: 10px;
            --box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0f4f8;
            color: var(--dark-text);
            line-height: 1.6;
            padding: 20px 0;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .card {
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 30px;
            margin-bottom: 30px;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
        }

        h1 {
            color: var(--primary-color);
            margin-bottom: 10px;
            font-size: 2.2rem;
        }

        .subtitle {
            color: #7f8c8d;
            font-size: 1.1rem;
        }

        .form-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }

        @media (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--primary-color);
        }

        input, select, textarea {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #bdc3c7;
            border-radius: var(--border-radius);
            font-size: 1rem;
            transition: var(--transition);
            background-color: white;
        }

        input:focus, select:focus, textarea:focus {
            border-color: var(--secondary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
        }

        .radio-group {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-top: 10px;
        }

        .radio-item {
            display: flex;
            align-items: center;
            cursor: pointer;
        }

        .radio-item input {
            width: auto;
            margin-right: 8px;
        }

        button {
            width: 100%;
            padding: 14px;
            background-color: var(--secondary-color);
            color: white;
            border: none;
            border-radius: var(--border-radius);
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            margin-top: 10px;
        }

        button:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(41, 128, 185, 0.3);
        }

        button:disabled {
            background-color: #95a5a6;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin: 30px 0;
        }

        .spinner {
            border: 5px solid rgba(0, 0, 0, 0.1);
            border-left-color: var(--secondary-color);
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .result-area {
            margin-top: 30px;
            padding: 25px;
            background-color: var(--light-bg);
            border-radius: var(--border-radius);
            border-left: 5px solid var(--accent-color);
            display: none;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .result-area h2 {
            color: var(--primary-color);
            margin-bottom: 15px;
            font-size: 1.5rem;
        }

        .result-content {
            white-space: pre-wrap;
            line-height: 1.8;
        }

        .result-content strong {
            color: var(--primary-color);
        }

        .result-content ul {
            margin-left: 20px;
            margin-bottom: 15px;
        }

        .error {
            color: #e74c3c;
            background-color: #fadbd8;
            border-color: #e57373;
            padding: 15px;
            border-radius: var(--border-radius);
            margin-top: 20px;
            display: none;
        }

        .disclaimer {
            margin-top: 30px;
            padding: 15px;
            background-color: #f1f8e9;
            border-radius: var(--border-radius);
            font-size: 0.9rem;
            color: #558b2f;
            border-left: 4px solid var(--accent-color);
        }

        footer {
            text-align: center;
            margin-top: 40px;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <header>
                <h1>💹 个性化投资模拟AI</h1>
                <p class="subtitle">输入您的投资信息，获取专业、个性化的投资建议</p>
            </header>

            <form id="investmentForm">
                <div class="form-grid">
                    <div class="form-group">
                        <label for="                            investment_amount">可投资金额 (元) *</label>
                        <input type="number" id="investment_amount" name="investment_amount" min="1000" placeholder="请输入您的可投资金额" required>
                    </div>

                    <div class="form-group">
                        <label>风险承受能力 *</label>
                        <div class="radio-group">
                            <label class="radio-item">
                                <input type="radio" name="risk_tolerance" value="保守型" required> 保守型
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="risk_tolerance" value="稳健型" required> 稳健型
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="risk_tolerance" value="平衡型" required> 平衡型
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="risk_tolerance" value="进取型" required> 进取型
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="risk_tolerance" value="激进型" required> 激进型
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="investment_period">投资期限 *</label>
                        <select id="investment_period" name="investment_period" required>
                            <option value="">请选择投资期限</option>
                            <option value="短期 (1年内)">短期 (1年内)</option>
                            <option value="中期 (1-3年)">中期 (1-3年)</option>
                            <option value="中长期 (3-5年)">中长期 (3-5年)</option>
                            <option value="长期 (5年以上)">长期 (5年以上)</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="investment_goal">主要投资目标 *</label>
                        <select id="investment_goal" name="investment_goal" required>
                            <option value="">请选择投资目标</option>
                            <option value="资产保值">资产保值</option>
                            <option value="稳健增值">稳健增值</option>
                            <option value="子女教育储备">子女教育储备</option>
                            <option value="退休养老储备">退休养老储备</option>
                            <option value="购房首付积累">购房首付积累</option>
                            <option value="财富快速增长">财富快速增长</option>
                            <option value="其他目标">其他目标</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label>金融知识水平 *</label>
                        <div class="radio-group">
                            <label class="radio-item">
                                <input type="radio" name="financial_knowledge" value="入门级" required> 入门级
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="financial_knowledge" value="基础级" required> 基础级
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="financial_knowledge" value="进阶级" required> 进阶级
                            </label>
                            <label class="radio-item">
                                <input type="radio" name="financial_knowledge" value="专业级" required> 专业级
                            </label>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="existing_investments">现有投资情况 (可选)</label>
                    <textarea id="existing_investments" name="existing_investments" rows="3" placeholder="例如：股票30%，基金40%，存款30%&#10;可描述您目前持有的投资产品、比例及大致金额"></textarea>
                </div>

                <button type="submit" id="submitBtn">🚀 生成投资建议</button>
            </form>

            <div class="loading" id="loading">
                <div class="spinner"></div>
                <p>正在为您生成个性化的投资建议，请稍候...</p>
            </div>

            <div class="error" id="errorMessage"></div>

            <div class="result-area" id="resultArea">
                <h2>📊 您的个性化投资建议</h2>
                <div class="result-content" id="resultContent"></div>
            </div>

            <div class="disclaimer">
                <strong>免责声明：</strong> 本工具提供的所有投资建议仅供参考，不构成任何投资决策依据。投资有风险，入市需谨慎。重大投资决策请咨询专业投资顾问。
            </div>
        </div>

        <footer>
            <p>© 2025 个性化投资模拟AI | 数据仅供参考，不构成投资建议</p>
        </footer>
    </div>

    <script>
        document.getElementById('investmentForm').addEventListener('submit', async function(e) {
            e.preventDefault(); // 阻止表单默认提交

            // 获取表单数据
            const formData = new FormData(this);
            const data = {};
            formData.forEach((value, key) => {
                // 处理单选按钮组
                if (key === 'risk_tolerance' || key === 'financial_knowledge') {
                    if (formData.getAll(key).length > 0) {
                        data[key] = formData.get(key);
                    }
                } else {
                    data[key] = value;
                }
            });

            // 简单验证
            if (!data.investment_amount || !data.risk_tolerance || !data.investment_period || !data.investment_goal || !data.financial_knowledge) {
                alert('请填写所有必填项 (*)。');
                return;
            }

            const amount = parseInt(data.investment_amount, 10);
            if (isNaN(amount) || amount < 1000) {
                alert('请输入有效的可投资金额 (至少1000元)。');
                return;
            }

            // 禁用按钮，显示加载动画
            const submitBtn = document.getElementById('submitBtn');
            submitBtn.disabled = true;
            submitBtn.textContent = '分析中...';
            document.getElementById('loading').style.display = 'block';
            document.getElementById('resultArea').style.display = 'none';
            document.getElementById('errorMessage').style.display = 'none';

            try {
                console.log('发送请求数据:', data); // 调试用

                // 发送POST请求到后端API
                const response = await fetch('/api/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                if (!response.ok) {
                   throw new Error(`网络响应错误: ${response.status} ${response.statusText}`);
                }

                const result = await response.json();
                console.log('收到响应:', result); // 调试用

                if (result.success) {
                    // 显示成功结果，将Markdown格式转换为HTML显示
                    const resultContent = document.getElementById('resultContent');
                    resultContent.innerHTML = result.result
                        .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
                        .replace(/\*(.*?)\*/g, '<em>$1</em>')
                        .replace(/# (.*?)\n/g, '<h3>$1</h3>')
                        .replace(/## (.*?)\n/g, '<h4>$1</h4>')
                        .replace(/### (.*?)\n/g, '<h5>$1</h5>')
                        .replace(/\n- (.*?)\n/g, '\n<ul><li>$1</li></ul>\n')
                        .replace(/<\/ul>\n<ul>/g, '');
                    
                    document.getElementById('resultArea').style.display = 'block';
                } else {
                    // 显示后端返回的错误
                    throw new Error(result.error || '未知错误');
                }
            } catch (error) {
                console.error('请求失败:', error);
                // 显示错误信息
                document.getElementById('errorMessage').textContent = `请求失败: ${error.message}`;
                document.getElementById('errorMessage').style.display = 'block';
            } finally {
                // 恢复按钮状态
                submitBtn.disabled = false;
                submitBtn.textContent = '🚀 生成投资建议';
                document.getElementById('loading').style.display = 'none';
            }
        });
    </script>
</body>
</html>